<template>
  <div class="system-log-container">
    <div class="spaceForm">
      <el-form :inline="true" :model="queryForm" @submit.native.prevent>
        <el-form-item>
          <el-select
            v-model="queryForm.cid"
            clearable
            placeholder="请选择公告类型"
            @change="queryData"
          >
            <el-option
              v-for="item in typeList"
              :key="item.id"
              :label="item.cat_title"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model.trim="queryForm.keyword"
            clearable
            placeholder="请输入关键词"
            @keyup.enter.native="queryData"
            @clear="queryData"
          />
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="primary" @click="queryData">
            查询
          </el-button>
        </el-form-item>
      </el-form>
      <el-form>
        <el-button
          icon="el-icon-plus"
          type="primary"
          @click="handleEdit($event)"
        >
          添加
        </el-button>
        <el-button type="danger" @click="handleDelete($event)">
          批量删除
        </el-button>
      </el-form>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      @selection-change="setSelectRows"
    >
      <el-table-column align="center" type="selection" />
      <el-table-column align="center" label="公告ID" prop="id" width="80px" />
      <el-table-column
        align="center"
        label="公告类型"
        prop="notice_cat_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="公告标题"
        prop="notice_title"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="公告内容"
        prop="notice_content"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="发布时间"
        prop="publish_time_format"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="浏览次数"
        prop="view_number"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="排序"
        prop="sort"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="操作"
        show-overflow-tooltip
        width="85"
      >
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">详情</el-button>
          <el-button type="text" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-image
          class="vab-data-empty"
          :src="require('@/assets/empty_images/data_empty.png')"
        />
      </template>
    </el-table>
    <notice-detail ref="noticeDetail" @fetch-data="fetchData" />
    <el-pagination
      background
      :current-page="queryForm.page"
      :layout="layout"
      :page-size="queryForm.perpage"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
  </div>
</template>

<script>
  import { getNotice, deleteNotice, getNoticeType } from '@/api/notice'
  import noticeDetail from './components/detail'
  export default {
    name: 'Notice',
    components: { noticeDetail },
    data() {
      return {
        list: [],
        listLoading: true,
        detail: '',
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          keyword: '',
          page: 1,
          perpage: 20,
        },
        typeList: [],
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      setSelectRows(val) {
        this.selectRows = val
      },
      handleSizeChange(val) {
        this.queryForm.perpage = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.page = val
        this.fetchData()
      },
      handleDelete(row) {
        if (row.id) {
          this.$baseConfirm('你确定要删除当前公告吗', null, async () => {
            const { data } = await deleteNotice({ ids: row.id })
            if (data.is_success) {
              this.$baseMessage(
                '删除成功',
                'success',
                'vab-hey-message-success'
              )
              await this.fetchData()
            }
          })
        } else {
          if (this.selectRows.length > 0) {
            const ids = this.selectRows.map((item) => item.id).join()
            this.$baseConfirm('你确定要删除选中公告吗', null, async () => {
              const { data } = await deleteNotice({ ids })
              if (data.is_success) {
                this.$baseMessage(
                  '删除成功',
                  'success',
                  'vab-hey-message-success'
                )
                await this.fetchData()
              }
            })
          } else {
            this.$baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      },
      handleEdit(row) {
        if (row.id) {
          this.$refs['noticeDetail'].showEdit(row)
        } else {
          this.$refs['noticeDetail'].showEdit()
        }
      },
      queryData() {
        this.queryForm.page = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const {
          data: { items, total },
        } = await getNotice(this.queryForm)
        this.list = items
        this.total = total
        this.listLoading = false
        this.getType()
      },
      async getType() {
        const { data } = await getNoticeType({})
        this.typeList = data.items
      },
    },
  }
</script>
